package com.app.newiptv.adaper;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.app.newiptv.R;
import com.app.newiptv.presenter.SearchPresenter;
import com.app.newiptv.utils.FocusedViewUtils;


/**
 * Created by Shinelon on 2017/9/24.
 */

public class KeyboardAdapter extends RecyclerView.Adapter<KeyboardAdapter.MainRecycleViewHolder> {


    private SearchPresenter presenter;
    private int count;
    private ImageView focusview;
    public KeyboardAdapter(SearchPresenter presenter,ImageView focus_view) {
        this.presenter = presenter;
        this.focusview = focus_view;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public MainRecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_keyboard_view, null, false);
        MainRecycleViewHolder mainRecycleViewHolder = new MainRecycleViewHolder(view);
        return mainRecycleViewHolder;
    }

    @Override
    public void onBindViewHolder(MainRecycleViewHolder holder, int position) {
        presenter.onBindKeyBoardChanelView(holder, position);
        holder.setPosition(position);
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public class MainRecycleViewHolder extends RecyclerView.ViewHolder implements SearchPresenter.MainViewHolder {

        private int position;
        private ImageView key_btn_view;
        private RelativeLayout key_bg_view;

        public MainRecycleViewHolder(View itemView) {
            super(itemView);
            key_btn_view = (ImageView) itemView.findViewById(R.id.key_btn_view);
            key_bg_view = (RelativeLayout) itemView.findViewById(R.id.key_bg_view);
            itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (b) {
                        boolean scroll = FocusedViewUtils.arrowScroll(view);
                        if(!scroll) {
                            FocusedViewUtils.setFocusedView(focusview, key_bg_view, 10, 10, 10, 10);
                        }
                    } else {
                        FocusedViewUtils.setBlurView(focusview,key_bg_view);
                        focusview.setVisibility(View.INVISIBLE);
                    }
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    presenter.keyboardPosition(position,v);
                }
            });
        }
        public void setPosition(int position){
            this.position = position;
        }

        @Override
        public void setkeyboardImg(int image) {
            key_btn_view.setImageResource(image);
        }
    }
}
